package com.mzz.creational.singleton;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * （7） 枚举单例扩展 保证 职责单一原则
 *
 *   备注：内部初始化对象时，可以加载其它配置信息，从而达到不同的目的
 * 
 * @author xushijian
 *
 */
public class SingletonEnumReload7 {

    public enum SingletonEnum {
        ConnectionFactory;
        private Connection connection;

        private SingletonEnum() {
            try {
                System.out.print("调用了Singleton的构造！");
                String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false";
                String username = "root";
                String password = "123456";
                String driverName = "com.mysql.cj.jdbc.Driver";
                Class.forName(driverName);
                connection = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        public Connection getConnection() {
            return connection;
        }

    }

    public static Connection getConnection() {
        return SingletonEnum.ConnectionFactory.getConnection();
    }

}
